package com.stu.util;

import com.stu.dao.RowMapper;
import com.stu.dao.impl.DeptRowMapper;
import com.stu.dao.impl.EmpRowMapper;
import com.stu.entity.Dept;
import com.stu.entity.Emp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author zy
 * @date 2025/3/4 13:58
 */
public class JdbcTemplate<T> {
    private RowMapper<T> rowMapper;

    public JdbcTemplate() {
    }

    public JdbcTemplate(RowMapper<T> rowMapper) {
        this.rowMapper = rowMapper;
    }

    /**
     * 数据库的增删改
     * @param sql 需要执行的SQL语句
     * @param params 占位符的实际参数
     * @return 修改的行数
     */
    public int update(String sql, Object... params) {
        int rows = 0;
        Connection conn = null;
        PreparedStatement ps = null;

        conn = ConnectionUtil.getConnection();  // 加载驱动和连接数据库

        try {
            ps = conn.prepareStatement(sql);  // 执行SQL语句
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);  // 放入SQL语句中占位符?里面的内容
            }
            rows = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if(ps != null) {
                try {
                    ps.close();  // 释放资源
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            ConnectionUtil.closeConnection(conn);  // 释放资源,if判断在方法里
        }
        return rows;
    }

    public T selectOne(String sql, Object... params) {
        T t = null;
        Connection conn = ConnectionUtil.getConnection();  // 连接
        PreparedStatement ps = null;  // 执行sql语句
        ResultSet rs = null;  // 结果集

        try {
            ps = conn.prepareStatement(sql);
            for(int i=0;i< params.length;i++){
                ps.setObject(i+1,params[i]);
            }
            rs = ps.executeQuery();

            if(rs.next()) {
                t = rowMapper.mapper(rs);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return t;
    }

    public static void main(String[] args) {
        JdbcTemplate jt = new JdbcTemplate();
        String sql1 = "insert into dept values (?,?,?)";  // 插入
        String sql2 = "delete from dept where deptno = ?";  // 删除
        int row = 0;
//        row  = JdbcTemplate.update(sql1,100,"人事部","苏州");
//        row = jt.update(sql2,100);
//        System.out.println(row);

        RowMapper<Dept> rowMapper = new DeptRowMapper();  // 模板参数构造
        String sql3 = "select * from dept where deptno = ?";  // sql语句
        JdbcTemplate<Dept> jt1 = new JdbcTemplate(rowMapper);  // jdbc模板构造
        Dept dept = jt1.selectOne(sql3,70);
        System.out.println(dept);

        RowMapper<Emp> rowMapper2 = new EmpRowMapper();
        String sql4 = "select * from emp where empno = ?";
        JdbcTemplate<Emp> jt2 = new JdbcTemplate(rowMapper2);
        Emp emp = jt2.selectOne(sql4,7990);
        System.out.println(emp);

        System.out.println("-----------------------");

        

    }
}
